package com.firstresponder.util;

import android.telephony.SmsManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.util.Log;

public class MessageSender {

	// TODO: CREATE A BROADCAST RECEIVER THAT CATCHES MESSAGES THAT FAILED AND
	// RESEND THEM. The broadcast receiver will be the pending intent placed in
	// the sendTextMesssage sentIntent or deliveryIntent parameters.

	private static final String TAG = "FRMessageSender";
	private static final String RESPONSE_YES = "YES";
	private static final String RESPONSE_INF_YES = "INF_YES";
	private static final String RESPONSE_NO = "NO";
	private static final String RESPONSE_INF_NO = "INF_NO";
	private static final String DEFAULT_NUMBER = "0000000000";
	private static MessageSender instance = new MessageSender();
	private final SmsManager smsmanager = SmsManager.getDefault();
	private static String SmsNumber = DEFAULT_NUMBER;
	// If the Yes and noNumber were not set then apply the
	private String yesNumber = DEFAULT_NUMBER;
	private String noNumber = DEFAULT_NUMBER;
	private SimpleDateFormat timingFormat;

	/**
	 * Sends an SMS message containing a bluetooth device in the vicinity of the
	 * sending device
	 * 
	 * @param address
	 *            :The Bluetooth Mac address of the discovered device
	 * @param locId
	 *            :The Bluetooth Mac address of the device sending the message
	 * @param action
	 *            :"ADD" for adding address to apparatus and "DELETE" for
	 *            deleting address from apparatus
	 */
	public void sendBluetoothMessage(String address, String locId, String action) {
		timingFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String messageText = "FR" + "#MAC=" + address + "#LOCID=" + locId
				+ "#TASK=" + action + "#TIME="
				+ timingFormat.format(new Date()) + "#FR";
		Log.i(TAG, "Bluetooth SMS Sent: " + messageText);
		try {
			smsmanager
					.sendTextMessage(SmsNumber, null, messageText, null, null);
		} catch (Throwable t) {
			Log.e(TAG, "phone number: " + SmsNumber);
			Log.e(TAG, Log.getStackTraceString(t));
		}
	}

	/**
	 * Sends a Location Update SMS Message to the Server
	 * 
	 * @param Latitude
	 *            :Latitude of Location
	 * @param Longitude
	 *            :Longitude of Location
	 * @param type
	 *            :"GPS" if received from gps and "NET" if received from Network
	 */
	public void sendLocUpdateMessage(Double lat, Double lon, String type) {
		String Latitude = Double.toString(lat);
		String Longitude = Double.toString(lon);
		timingFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String messageText = "FR" + "#LOC=" + type + "#LAT=" + Latitude
				+ "#LNG=" + Longitude + "#TIME="
				+ timingFormat.format(new Date()) + "#FR";
		Log.i(TAG, "Location SMS Sent: " + messageText);
		try {
			smsmanager
					.sendTextMessage(SmsNumber, null, messageText, null, null);
		} catch (Throwable t) {
			Log.e(TAG, "phone number: " + SmsNumber);
			Log.e(TAG, Log.getStackTraceString(t));
		}
	}

	/**
	 * Sends a SMS message stating that there is no available Location
	 */
	public void sendUnableToLocate() {
		timingFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String messageText = "FR" + "#LOC=NONE#LAT=0#LNG=0#TIME="
				+ timingFormat.format(new Date()) + "#FR";
		Log.i(TAG, "Location SMS Sent: " + messageText);
		try {
			smsmanager
					.sendTextMessage(SmsNumber, null, messageText, null, null);
		} catch (Throwable t) {
			Log.e(TAG, "phone number: " + SmsNumber);
			Log.e(TAG, Log.getStackTraceString(t));
		}
	}

	/**
	 * Sends the Responding Status to the Server
	 * 
	 * @param status
	 *            : String that represents the response status; options
	 *            restricted to: "YES" and "INF_YES"
	 */
	public void sendYesResponseMessage(String status) {
		timingFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String messageText = "FR" + "#STA=" + status + "#TIME="
				+ timingFormat.format(new Date()) + "#FR";
		Log.i(TAG, "Responding Status SMS Sent: " + messageText);
		Log.i(TAG, "Destination Address " + yesNumber);
		try {
			smsmanager
					.sendTextMessage(yesNumber, null, messageText, null, null);
		} catch (Throwable t) {
			Log.e(TAG, "phone number: " + yesNumber);
			Log.e(TAG, Log.getStackTraceString(t));
		}
	}

	/**
	 * 
	 * @param status
	 *            : String that represents the response status; options
	 *            restricted to: "NO" and "INF_NO"
	 */
	public void sendNoResponseMessage(String status) {
		timingFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String messageText = "FR" + "#STA=" + status + "#TIME="
				+ timingFormat.format(new Date()) + "#FR";
		Log.i(TAG, "Responding Status SMS Sent: " + messageText);
		try {
			smsmanager.sendTextMessage(noNumber, null, messageText, null, null);
		} catch (Throwable t) {
			Log.e(TAG, "phone number: " + noNumber);
			Log.e(TAG, Log.getStackTraceString(t));
		}
	}

	public static synchronized MessageSender getInstance() {
		if (instance == null) {
			instance = new MessageSender();
		}
		return instance;
	}

	public void setSmsNumber(String destinationAddress) {
		String oldNumber = SmsNumber;
		MessageSender.SmsNumber = destinationAddress;
		if ((yesNumber == oldNumber && noNumber == oldNumber)
				|| (yesNumber.equals("0000000000") && noNumber
						.equals("0000000000"))) {
			yesNumber = SmsNumber;
			noNumber = SmsNumber;
		}
	}

	public void setYesNumber(String yesNumber) {
		this.yesNumber = yesNumber;
	}

	public void setNoNumber(String noNumber) {
		this.noNumber = noNumber;
	}

}
